package cn.iocoder.yudao.module.ai.controller.admin.creationcategory;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ai.controller.admin.creationcategory.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.creationcategory.CreationCategoryDO;
import cn.iocoder.yudao.module.ai.dal.dataobject.creationmodel.CreationModelDO;
import cn.iocoder.yudao.module.ai.service.creationcategory.CreationCategoryService;

@Tag(name = "管理后台 - 创作类别")
@RestController
@RequestMapping("/ai/creation-category")
@Validated
public class CreationCategoryController {

    @Resource
    private CreationCategoryService creationCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建创作类别")
    @PreAuthorize("@ss.hasPermission('ai:creation-category:create')")
    public CommonResult<Long> createCreationCategory(@Valid @RequestBody CreationCategorySaveReqVO createReqVO) {
        return success(creationCategoryService.createCreationCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新创作类别")
    @PreAuthorize("@ss.hasPermission('ai:creation-category:update')")
    public CommonResult<Boolean> updateCreationCategory(@Valid @RequestBody CreationCategorySaveReqVO updateReqVO) {
        creationCategoryService.updateCreationCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除创作类别")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:creation-category:delete')")
    public CommonResult<Boolean> deleteCreationCategory(@RequestParam("id") Long id) {
        creationCategoryService.deleteCreationCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得创作类别")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:creation-category:query')")
    public CommonResult<CreationCategoryRespVO> getCreationCategory(@RequestParam("id") Long id) {
        CreationCategoryDO creationCategory = creationCategoryService.getCreationCategory(id);
        return success(BeanUtils.toBean(creationCategory, CreationCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得创作类别分页")
    @PreAuthorize("@ss.hasPermission('ai:creation-category:query')")
    public CommonResult<PageResult<CreationCategoryRespVO>> getCreationCategoryPage(@Valid CreationCategoryPageReqVO pageReqVO) {
        PageResult<CreationCategoryDO> pageResult = creationCategoryService.getCreationCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CreationCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出创作类别 Excel")
    @PreAuthorize("@ss.hasPermission('ai:creation-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCreationCategoryExcel(@Valid CreationCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CreationCategoryDO> list = creationCategoryService.getCreationCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "创作类别.xls", "数据", CreationCategoryRespVO.class,
                        BeanUtils.toBean(list, CreationCategoryRespVO.class));
    }

    // ==================== 子表（创作模型） ====================

    @GetMapping("/creation-model/page")
    @Operation(summary = "获得创作模型分页")
    @Parameter(name = "categoryId", description = "类别id")
    @PreAuthorize("@ss.hasPermission('ai:creation-category:query')")
    public CommonResult<PageResult<CreationModelDO>> getCreationModelPage(PageParam pageReqVO,
                                                                                        @RequestParam("categoryId") Long categoryId) {
        return success(creationCategoryService.getCreationModelPage(pageReqVO, categoryId));
    }

    @PostMapping("/creation-model/create")
    @Operation(summary = "创建创作模型")
    @PreAuthorize("@ss.hasPermission('ai:creation-category:create')")
    public CommonResult<Long> createCreationModel(@Valid @RequestBody CreationModelDO creationModel) {
        return success(creationCategoryService.createCreationModel(creationModel));
    }

    @PutMapping("/creation-model/update")
    @Operation(summary = "更新创作模型")
    @PreAuthorize("@ss.hasPermission('ai:creation-category:update')")
    public CommonResult<Boolean> updateCreationModel(@Valid @RequestBody CreationModelDO creationModel) {
        creationCategoryService.updateCreationModel(creationModel);
        return success(true);
    }

    @DeleteMapping("/creation-model/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除创作模型")
    @PreAuthorize("@ss.hasPermission('ai:creation-category:delete')")
    public CommonResult<Boolean> deleteCreationModel(@RequestParam("id") Long id) {
        creationCategoryService.deleteCreationModel(id);
        return success(true);
    }

	@GetMapping("/creation-model/get")
	@Operation(summary = "获得创作模型")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:creation-category:query')")
	public CommonResult<CreationModelDO> getCreationModel(@RequestParam("id") Long id) {
	    return success(creationCategoryService.getCreationModel(id));
	}

}